/**
 * Created by Maxwell on 2016/9/26.
 */
"use strict";

let router = require('koa-router')();
let config = require("../../config");
let commonService = require('../../service/common/service');


router.get('/login', function *() {
    yield this.render('common/login');
});

router.post('/common/login', function*() {
    var params = this.request.body;
    var userInfo;
    var token = JSON.parse(yield commonService.login({
        "grant_type": 'password',
        "username": params.username,
        "password": params.password
    }));
    userInfo = yield commonService.getCurrentUserInfo({
        token: token
    });
    this.session.userInfo = userInfo;
    this.session.token = token;
    try {
        if (token.access_token) {
            this.body = {
                status: 'succeed',
                msg: '登录成功',
                access_token: token.access_token
            }
        } else {
            this.body = {
                status: 'error',
                msg: '登录失败'
            }
        }
    } catch (e) {
        this.body = {
            status: 'error',
            msg: '异常错误'
        }
    }
});

router.get('/error/sysError', function *() {
    yield this.render('sysError');
});

module.exports = router;